new Vue({
    el:"#app",
    data: {
        fullscreenLoading:false,
        screenLoading:false,
        strategyData:[],
        dialogFormVisible: false,
        queryContent:{
            keyword:'',
            date:[]
        },
        pageNo: 1,
        pageSize: 15,
        total: 0,
        pages: 0,
        id: ''
    },
    mounted:function(){
        var _this=this;
        _this.loadStrategyList();
    },
    methods:{
        trim:function(str){
            if(isNaN(str)){
                return str.replace(/(^\s*)|(\s*$)/g, "");
            }else{
                return str
            }
        },
        loadStrategyList:function () {
            var _this = this;
            _this.screenLoading=true;
            var startDate = '';
            var endDate = '';
            if(_this.queryContent.date != null && _this.queryContent.date[0] != null) {
                startDate = _this.easyFormatDate(_this.queryContent.date[0]);
                endDate = _this.easyFormatDate(_this.queryContent.date[1]);
            }

            axios.get(request_prefix+"/cash/strategy/list",{
                params: {
                    currentPage: _this.pageNo,
                    keyword: _this.trim(_this.queryContent.keyword),
                    startDate: startDate,
                    endDate: endDate
                }
            })
                .then(function(res){
                    if(res.data.code===0){
                        _this.pageNo = parseInt(res.data.data.pageNo);
                        _this.total = parseInt(res.data.data.total);
                        _this.pageSize = parseInt(res.data.data.pageSize);
                        _this.pages = parseInt(res.data.data.pages);
                        _this.strategyData = res.data.data.dataList;
                        _this.screenLoading = false;
                        return;
                    }else{
                        _this.screenLoading=false;
                        _this.$message.error("获取数据失败");
                        return;
                    }
                })
                .catch(function(res){
                    _this.screenLoading=false;
                    _this.$message.error("请求失败");
                    return;
                })
        },
        resetQuery:function () {
            var _this = this;
            _this.queryContent.keyword = '';
            _this.queryContent.date = [];
            _this.pageNo = 1;
            _this.loadStrategyList();
        },
        //分页
        handleCurrentChange:function(val) {
            var _this=this;
            var startDate = '';
            var endDate = '';
            if(_this.queryContent.date != null  && _this.queryContent.date[0] != null) {
                startDate = _this.easyFormatDate(_this.queryContent.date[0]);
                endDate = _this.easyFormatDate(_this.queryContent.date[1]);
            }
            axios.get(request_prefix+"/cash/strategy/list",{
                params: {
                    currentPage: _this.pageNo,
                    keyword: _this.trim(_this.queryContent.keyword),
                    startDate: startDate,
                    endDate: endDate
                }
            })
                .then(function(res){
                    if(res.data.code===0){
                        _this.pageNo = parseInt(res.data.data.pageNo);
                        _this.total = parseInt(res.data.data.total);
                        _this.pageSize = parseInt(res.data.data.pageSize);
                        _this.pages = parseInt(res.data.data.pages);
                        _this.strategyData = res.data.data.dataList;
                    }else{
                        _this.$message.error("请求数据失败")
                    }
                })
                .catch(function(res){
                    _this.$message.error("请求失败")
                })
        },
        toNewPage:function () {
            window.location.href = request_prefix+"/cash/strategy/createPage";
        },
        toEditPage:function (id) {
            localStorage.setItem("s_id_",id);
            window.location.href = request_prefix+"/cash/strategy/editPage";
        },
        easyFormatDate: function(date) {
            var year = date.getFullYear();
            var month = (date.getMonth() + 1) < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1 + "";
            var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate() + "";
            return year + "-" + month + "-" + day;
        },
        deleteStrategy:function (id) {
            var _this = this;
            _this.id = id;
            _this.dialogFormVisible = true;
        },
        confirmDelete:function () {
            var _this = this;
            var id = parseInt(_this.id);

            var data = new URLSearchParams();
            data.append("id",id);

            axios.post(request_prefix+"/cash/strategy/delete",data)
                .then(function (res) {
                    if(res.data.code === 0) {
                        for (var i = 0;i < _this.strategyData.length;i++) {
                            if(id == parseInt(_this.strategyData[i].id)) {
                                _this.strategyData.splice(i,1);
                            }
                        }
                        _this.dialogFormVisible = false;
                    } else {
                        _this.$message.error("删除失败");
                    }
                })
                .catch(function (res) {
                    _this.$message.error("请求过程错误")
                })
        },
        toStrategyDetail:function (id) {
            localStorage.setItem("s_id_",id);
            window.location.href = request_prefix+"/cash/strategy/detailPage";
        }
    }
});